package coder.nav.schedule.action;

import coder.nav.utils.MdStyleUtils;
import coder.nav.model.bo.NavTaskActionBO;
import coder.nav.model.vo.setting.NavNoticeSettingVO;
import coder.nav.sdk.WxPusherAPI;
import coder.nav.service.NavMailService;
import coder.nav.service.NavSettingService;
import coder.nav.service.NavTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Component
public class NavTaskNoticeAction extends NavTaskBaseAction {

    @Autowired
    protected NavSettingService settingService;
    @Autowired
    protected NavTaskService taskService;
    @Autowired
    protected NavMailService mailService;
    @Autowired
    protected RedisTemplate<String, String> redisTemplate;

    @Override
    public String title() {
        return "消息推送 - Markdown";
    }

    @Override
    public String context() {
        return super.context();
    }

    @Override
    public String params() {
        return super.params();
    }

    @Override
    public Date executeTime() {
        return super.executeTime();
    }

    @Override
    public NavTaskActionBO getEntity(Long userId) {
        return super.getEntity(userId);
    }

    @Override
    public void addTask(Long userId) {
        NavTaskActionBO entity = this.getEntity(userId);
        taskService.saveTaskAction(entity);
    }

    @Override
    public void actionTask(NavTaskActionBO task) {
        this.actionTask(task, null, null);
    }

    public void actionTask(NavTaskActionBO task, Integer noticeType, String fileSuf) {
        try {
            if(StringUtils.isBlank(task.getContext().trim())) {
                return; // 发送内容为空，跳过
            }
            NavNoticeSettingVO noticeSetting = settingService.getNoticeSetting(task.getUserId());
            if(Objects.isNull(noticeType)) {
                // 默认邮箱发送消息
                noticeType = noticeSetting.getNoticeType();
            }
            // Markdown 转 Html
            if(task.isNeedToHtml()) {
                String markdown = task.getContext();
                String html = MdStyleUtils.markdownToStyledHtml(markdown);
                task.setContext(html);
            }
            // 消息发送
            switch(noticeType) {
                case 1: // 微信公众号
                    // WxPusherAPI.sendMessage(noticeSetting.getUid(), task.getTitle(), task.getContext());
                    break;
                default: // 邮件
                    if(StringUtils.isNotBlank(fileSuf)) {
                        mailService.sendFileMail(noticeSetting.getEmail(), task.getTitle(), task.getContext(), fileSuf);
                        break;
                    }
                    mailService.sendHtmlMail(noticeSetting.getEmail(), task.getTitle(), task.getContext());
            }
        } catch (Exception ignored) {
        } finally {
            taskService.deleteTaskAction(task.getActionId(), task.getUserId());
        }
    }

}
